package com.cwits.wifi.dvr;

import android.content.Context;
import android.content.Intent;

import com.cwits.cyx_drive_sdk.bean.ReceivedData;
import com.cwits.cyx_drive_sdk.dvr.DVRCSVUtil;
import com.cwits.cyx_drive_sdk.dvr.DVRManager;
import com.cwits.cyx_drive_sdk.dvr.OnDownloadFTPFileListener;
import com.cwits.cyx_drive_sdk.util.HttpUtil;
import com.cwits.wifi.util.Dbug;
import com.cwits.wifi.util.IAction;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by zhanghua on 2016/7/8.
 */



public class DVRCarManager {
    private String tag = this.getClass().getSimpleName();

    private static DVRCarManager mDvrCarManager;//dvr数据处理类
    private static boolean mIsRegister = false;//是否已经注册标记
    private boolean mIsDownloading = false;//是否正在下载标记
    private boolean mIsUploading = false;//是否正在上传标记

    /**
     * 单例获取
     * @return
     */
    public static DVRCarManager getInstance() {
        synchronized (DVRCarManager.class) {
            if (mDvrCarManager == null) {
                mDvrCarManager = new DVRCarManager();
            }
        }
        return mDvrCarManager;
    }

    /**
     * 构造
     */
    private DVRCarManager(){

    }

    /**
     * 注册
     * @param uuid 设备或手机账号
     * @param downloadpath 下载路径
     * @param context 上下文
     */
    public void initRegisterInfo(String uuid,String downloadpath,Context context){
        if(!mIsRegister){
            DVRManager.getInstance().initRegisterInfo(uuid, downloadpath, context);
            mIsRegister = true;
        }
    }


    /**
     * 设置是否正在下载
     * @param isdownloading
     */
    public void setIsDownloading(boolean isdownloading){
        mIsDownloading = isdownloading;
    }

    /**
     * 获取是否正在下载
     * @return
     */
    public boolean getIsDownloading(){
        return mIsDownloading;
    }


    /**
     * 设置是否正在上传
     * @param isuploading
     */
    public void setIsUploading(boolean isuploading){
        mIsUploading = isuploading;
    }

    /**
     * 获取是否正在上传
     * @return
     */
    public boolean getIsUploading(){
        return mIsUploading;
    }

    /**
     *
     * @param host_name ip地址
     * @param port 端口号
     * @param name 用户名
     * @param password 密码
     * @param device_id 设备uuid
     * @param downloadlistener 下载监听
     */
    public void downloadFTPFiles(String host_name,int port,String name,String password,String device_id,
                                 OnDownloadFTPFileListener downloadlistener){
        DVRManager.getInstance().downloadFTPFiles(host_name, port, name, password, device_id, downloadlistener);
    }

    /**
     *
     * @param host_name ip地址
     * @param port 端口号
     * @param name 用户名
     * @param password 密码
     * @param device_id 设备uuid
     * @param downloadfilepath 设置下载的路径
     * @param downloadlistener 下载监听
     */
    public void downloadFTPFiles(String host_name,int port,String name,String password,String device_id,String downloadfilepath,
                                 OnDownloadFTPFileListener downloadlistener){
        DVRManager.getInstance().downloadFTPFiles(host_name, port, name, password, device_id, downloadfilepath, downloadlistener);
    }

    /**
     * 上传，在下载和上传之前必须注册用户账号：initRegisterInfo
     * @param uploadListener
     */
    public void uploadHTTPFiles(DVRCSVUtil.OnUploadStrokeListener uploadListener){
        DVRManager.getInstance().uploadHTTPFiles(uploadListener);
    }

    /**
     * 下载完成之后发送下载完成广播
     * @param context
     */
    public void sendFinishUploadBroadcast(Context context){
        Intent requestConnection = new Intent(IAction.ACTION_FINISH_UPLOAD);
        context.sendBroadcast(requestConnection);
    }

    /**
     * 上传完成之后发送上传完成广播
     * @param context
     */
    public void sendFinishDownloadBroadcast(Context context){
        Intent requestConnection = new Intent(IAction.ACTION_FINISH_DOWNLOAD);
        context.sendBroadcast(requestConnection);
    }

    /**
     * 获取一周的评分详情
     * @param uid
     * @param date
     * @param weekinfolistener
     */
    public void getDVRWeekDataInfo(final String uid,final String date,final OnStatWeekInfoListener weekinfolistener){
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                ReceivedData.StatWeekInfo weekinfo = HttpUtil.getStatWeek(uid, date);
                if(weekinfo != null){
                    if(weekinfolistener != null)
                        weekinfolistener.onSuccess(weekinfo);
                }else{
                    if(weekinfolistener != null)
                        weekinfolistener.onFailed(-1);
                }
            }
        }).start();
    }

    /**
     * 监听获取周信息
     * @author Administrator
     *
     */
    public interface OnStatWeekInfoListener{
         void onSuccess(ReceivedData.StatWeekInfo weekinfo);
         void onFailed(int errorcode);
    }

    /**
     * 一周的评分详情转json格式
     * @param weekinfo
     * @return
     */
    public String weekInfoToJson( ReceivedData.StatWeekInfo weekinfo){
        try{
            JSONObject object = new JSONObject();
            object.put("aece",weekinfo.aece);
            object.put("ave",weekinfo.ave);
            object.put("code",weekinfo.code);
            object.put("brake",weekinfo.brake);
            object.put("dece",weekinfo.dece);
            object.put("msg",weekinfo.msg);
            object.put("mile",weekinfo.mile);
            object.put("ntimes",weekinfo.ntimes);
            object.put("score",weekinfo.score);
            object.put("times",weekinfo.times);
            object.put("turn",weekinfo.turn);
            List<ReceivedData.DayStrokeInWeek> list = weekinfo.day;
            if(list != null&&list.size() > 0){
                JSONArray array = new JSONArray();
                for(int i = 0; i < list.size(); i++)
                {
                    JSONObject objectchild = new JSONObject();
                    objectchild.put("aece",list.get(i).aece);
                    objectchild.put("brake",list.get(i).brake);
                    objectchild.put("date",list.get(i).date);
                    objectchild.put("dece",list.get(i).dece);
                    objectchild.put("week",list.get(i).week);
                    objectchild.put("time",list.get(i).time);
                    objectchild.put("mile",list.get(i).mile);
                    objectchild.put("turn",list.get(i).turn);
                    objectchild.put("score",list.get(i).score);
                    array.put(objectchild);
                }
                object.put("data",array);
            }
            return object.toString();
        }catch(Exception e){
            Dbug.e("------------------weekInfoToJson====",e.getMessage());
            return null;
        }
    }

    /**
     * json格式数据转一周评分详情数据
     * @param weekinfoJson
     * @return
     */
    public ReceivedData.StatWeekInfo JsonToWeekInfo(String weekinfoJson){
        if(weekinfoJson == null)
            return null;
        try{

            ReceivedData.StatWeekInfo statWeek = new ReceivedData.StatWeekInfo();
            JSONObject object = new JSONObject(weekinfoJson);
            statWeek.aece = object.optInt("aece");
            statWeek.ave = (float)object.optDouble("ave");
            statWeek.brake = object.optInt("brake");
            statWeek.dece = object.optInt("dece");
            statWeek.msg = object.optString("msg");
            statWeek.mile = object.optInt("mile");
            statWeek.ntimes = object.optInt("ntimes");
            statWeek.score = object.optInt("score");
            statWeek.times = object.optInt("times");
            statWeek.turn = object.optInt("turn");

            String aar = object.optString("data");
            if(!aar.equals("")){
                JSONArray jsonarry = new JSONArray(aar);
                for(int i = 0;i < jsonarry.length();i++){
                    ReceivedData.DayStrokeInWeek dayinfo = new ReceivedData.DayStrokeInWeek();
                    JSONObject objectchild = jsonarry.getJSONObject(i);
                    dayinfo.week = objectchild.optInt("week");
                    dayinfo.aece = objectchild.optInt("aece");
                    dayinfo.brake = objectchild.optInt("brake");
                    dayinfo.date = objectchild.optString("date");
                    dayinfo.time = objectchild.optInt("time");
                    dayinfo.mile = objectchild.optInt("mile");
                    dayinfo.turn = objectchild.optInt("turn");
                    dayinfo.score = objectchild.optInt("score");
                    dayinfo.dece = objectchild.optInt("dece");

                    statWeek.day.add(dayinfo);
                }
            }

            return statWeek;
        }catch(Exception e){
            Dbug.e(tag,"---------------JsonToWeekInfo-----");
            return null;
        }
    }
}
